<template>
  <div id='register'>
      <p  class="register1">注册账号</p>
      <div class="box">
          <div class="left"><img src="../assets/image/account1.png"></div>
          <div class="right"><input type="text" v-model="account" placeholder="设置账号，注册后不可更改"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/phone.png"></div>
          <div class="right"><input type="text" v-model="phone" placeholder="输入手机号"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/password.png"></div>
          <div class="right"><input type="password" v-model="password" placeholder="请设置登录密码"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/password.png"></div>
          <div class="right"><input type="password" v-model="password1" placeholder="请确认登录密码"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/safepassword.png"></div>
          <div class="right"><input type="password" v-model="safepassword" placeholder="请设置安全密码"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/safepassword.png"></div>
          <div class="right"><input type="password" v-model="safepassword1" placeholder="请确认安全密码"></div>
      </div>
      <div class="box">
          <div class="left"><img src="../assets/image/Invitation.png"></div>
          <div class="right"><input type="text" v-model="Invitation" placeholder="邀请人账户"></div>
      </div>
      <div class="box box1">
          <div class="left"><img src="../assets/image/shoukuan.png"></div>
          <div class="right">
            <p>上传微信/支付宝收款码</p>
            <p>(微信收款码与支付宝收款码可任选其一)</p>
          </div>
      </div>
      <div class="uploadimg">
        <div class="upload"  @click="portrait1">
          <div class="yulan" v-if="img1"><img :src="`http://${img1}`"></div>
            <input class="imginp" ref="portrait1" name="imgurl" id="imgurl" type='file' accept="image/*" @change="shangchuan1"/>
            <div class="box">
              <i class="iconfont icon-tianjia" v-if="!img1"></i>
              <span>微信收款码</span>
            </div>
        </div>
        <div class="upload"  @click="portrait2">
            <div class="yulan"  v-if="img2"><img :src="`http://${img2}`"></div>
            <input class="imginp" ref="portrait2" name="imgurl" id="imgurl" type='file' accept="image/*" @change="shangchuan2"/>
            <div class="box">
              <i class="iconfont icon-tianjia" v-if="!img2"></i>
              <span>支付宝收款码</span>
            </div>
        </div>
      </div>
      
      <com-button :click="register">注册</com-button>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      account: "",
      phone: "",
      password: "",
      password1: "",
      safepassword: "",
      safepassword1: "",
      Invitation: "",
      img1: "", //微信
      imgid1: "",
      img2: "", //支付宝
      imgid2: ""
    };
  },

  computed: {},

  created() {},

  mounted() {},

  methods: {
    // 头像单击事件
    portrait1() {
      this.$refs.portrait1.click(); // 获取ref为portrait的元素相当于获取id为portrait的元素
    },
    portrait2() {
      this.$refs.portrait2.click(); // 获取ref为portrait的元素相当于获取id为portrait的元素
    },
    // 上传图片
    shangchuan1(e) {
      var self = this;
      let file = e.target.files[0]; // 上传照片
      /* eslint-disable no-undef */
      let formData = new FormData(); // 创建form对象
      formData.append("imgurl", file); // 通过append向form对象添加数据
      // formData.append("token", this.token()); // 添加form表单中其他数据
      console.log(formData.get("imgurl")); // FormData私有类对象，访问不到，可以通过get判断值是否传进去
      let config = {
        emulateJSON: true,
        withCredentials: false,
        headers: { "Content-Type": "multipart/form-data" }
      };
      this.$axios
        .post(
          "http://kuaibb.cadhx.com/index.php/index/apiupload/doupload",
          formData,
          config
        )
        .then(({ data }) => {
          if (data.code === "200") {
            
            this.img1 = data.img;
            this.imgid1 = data.imgid;
          } else if (data.code === "204") {
            this.$bus.$emit("toast", data.data);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    shangchuan2(e) {
      var self = this;
      let file = e.target.files[0]; // 上传照片
      /* eslint-disable no-undef */
      let formData = new FormData(); // 创建form对象
      formData.append("imgurl", file); // 通过append向form对象添加数据
      // formData.append("token", this.token()); // 添加form表单中其他数据
      console.log(formData.get("imgurl")); // FormData私有类对象，访问不到，可以通过get判断值是否传进去
      let config = {
        emulateJSON: true,
        withCredentials: false,
        headers: { "Content-Type": "multipart/form-data" }
      };
      this.$axios
        .post(
          "http://kuaibb.cadhx.com/index.php/index/apiupload/doupload",
          formData,
          config
        )
        .then(({ data }) => {
          if (data.code === "200") {
            
            this.img2 = data.img;
            this.imgid2 = data.imgid;
          } else if (data.code === "204") {
            this.$bus.$emit("toast", data.data);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    register() {
      var reg = /^[0-9a-zA-Z]+$/;
      let regTel = /^(1[3-9])\d{9}$/;
      if (
        !this.account ||
        !this.phone ||
        !this.password ||
        !this.safepassword ||
        !this.Invitation
      ) {
        this.$bus.$emit("toast", "输入信息不能为空");
      } else if (!reg.test(this.account)) {
        this.$bus.$emit("toast", "账户只能为数字或字母");
      } else if (!regTel.test(this.phone)) {
        this.$bus.$emit("toast", "手机号码不合法");
      } else if (this.password != this.password1) {
        this.$bus.$emit("toast", "俩次登陆密码不一致");
      } else if (this.safepassword != this.safepassword1) {
        this.$bus.$emit("toast", "俩次安全密码不一致");
      } else {
        this.axios
          .post("apireg/index", {
            user_num: this.account,
            phone: this.phone,
            pwd: this.password,
            sale_code: this.safepassword,
            pid: this.Invitation,
            alipay: this.imgid2,
            wechat: this.imgid1
          })
          .then(({ data }) => {
            
            if (data.code === "200") {
              this.$router.push("login");
              this.$bus.$emit("toast", data.data);
            } else if (data.code === "204") {
              this.$bus.$emit("toast", data.data);
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    }
  }
};
</script>
<style lang='scss'>
#register {
  width: 750px;
  // text-align: center;
  background-color: #fff;
  padding: 76px 60px 0px;
  .com_button {
    margin-top: 150px;
  }
  .register1 {
    margin-bottom: 62px;
    font-size: 36px;
    text-align: left;
    font-family: PingFangSC-Semibold;
    font-weight: 600;
    color: rgba(10, 137, 172, 1);
    line-height: 50px;
  }
  .box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      width: 36px;
      height: 40px;
      img {
        width: 100%;
      }
    }
    .right {
      border-bottom: 1Px solid #0a89ac;
      input {
        width: 570px;
        height: 110px;
        background: rgba(255, 255, 255, 0.1);
      }
      input::-webkit-input-placeholder {
        /* WebKit browsers */
        color: #aaa;
      }
      input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #aaa;
      }

      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #aaa;
      }

      input:-ms-input-placeholder {
        /* Internet Explorer 10+ */
        color: #aaa;
      }
    }
  }
  .box1 {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .left {
      width: 40px;
      height: 40px;
      img {
        width: 100%;
      }
    }
    .right {
      margin-top: 20px;
      margin-left: 24px;
      border-bottom: none;
      color: #0a89ac;
      text-align: left;
      p:nth-of-type(1) {
        padding-top: 28px;
        font-size: 30px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(10, 137, 172, 1);
        line-height: 42px;
      }
      p:nth-of-type(2) {
        font-size: 26px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(198, 217, 222, 1);
        line-height: 40px;
      }
    }
  }
  .uploadimg {
    display: flex;
    justify-content: space-around;
    align-items: center;
    .upload {
      width: 200px;
      height: 200px;
      margin-top: 20px;
      border-radius: 6px;
      border: 1Px dashed #0a89ac;
      .yulan {
        width: 198px;
        height: 198px;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          width: 100%;
          height: 100%;
        }
      }
      input {
        width: 10px;
        opacity: 0;
        filter: alpha(opacity=0);
      }
      .box {
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        span {
          font-size: 24px;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          color: rgba(10, 137, 172, 1);
          line-height: 60px;
        }
        .iconfont {
          font-size: 65px;
          color: #0a89ac;
        }
      }
    }
  }

  .register {
    margin-top: 80px;
    font-size: 30px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(10, 137, 172, 1);
    line-height: 42px;
  }
}
</style>